package com.example.onlinetaxi.view.Dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.example.onlinetaxi.R;

// 司机输入乘客手机号后四位进行验证
public class PhoneDialog extends Dialog {

    private PhoneDialog(Context context) {
        super(context);
    }

    private PhoneDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    private PhoneDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static void showInfoDialog(Context context,
                                      View.OnClickListener cancelOnClick,
                                      View.OnClickListener confirmOnClick) {
        Builder builder = new Builder(context);

        builder.setButtonCancel(cancelOnClick);
        builder.setButtonConfirm(confirmOnClick);

        PhoneDialog customDialog = builder.create();
        customDialog.show();
    }

    /* Builder */
    public static class Builder {
        // 文字框
        public static EditText num_in1_et, num_in2_et, num_in3_et, num_in4_et;
        // 取消按钮
        private Button btnCancel;
        private Button btnConfirm;

        private View mLayout;
        private View.OnClickListener mButtonCancelClickListener;
        private View.OnClickListener mButtonConfirmClickListener;

        private PhoneDialog mDialog;

        public Builder(Context context) {
            mDialog = new PhoneDialog(context, R.style.custom_dialog);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // 加载布局文件
            mLayout = inflater.inflate(R.layout.phone_dialog, null, false);
            // 添加布局文件到 Dialog
            mDialog.addContentView(mLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

            // 文本框初始化
            num_in1_et = mLayout.findViewById(R.id.num_in1_et);
            num_in2_et = mLayout.findViewById(R.id.num_in2_et);
            num_in3_et = mLayout.findViewById(R.id.num_in3_et);
            num_in4_et = mLayout.findViewById(R.id.num_in4_et);

            // 设置第一个输入框获得焦点
            num_in1_et.setFocusable(true);
            num_in1_et.setFocusableInTouchMode(true);
            num_in1_et.requestFocus();

            //addTextChangedListener设置监听文本变化，有输入跳转到下一个输入框
            num_in1_et.addTextChangedListener(new TextChangeLister(num_in1_et, num_in2_et));

            //setOnKeyListener设置监听键盘删除操作，删除完之后跳转到上一个输入框
            num_in2_et.setOnKeyListener(new MyOnKeyListener(num_in1_et, num_in2_et));

            num_in2_et.addTextChangedListener(new TextChangeLister(num_in2_et, num_in3_et));

            num_in3_et.setOnKeyListener(new MyOnKeyListener(num_in2_et, num_in3_et));
            num_in3_et.addTextChangedListener(new TextChangeLister(num_in3_et, num_in4_et));

            num_in4_et.setOnKeyListener(new MyOnKeyListener(num_in3_et, num_in4_et));

            // 按钮初始化
            btnCancel = mLayout.findViewById(R.id.phone_btn_cancel);
            btnCancel.setClickable(true);
            btnConfirm = mLayout.findViewById(R.id.phone_btn_confirm);
        }

        //自定义文本变化监听类
        class TextChangeLister implements TextWatcher {
            private EditText thisEt, nextEt;

            public TextChangeLister(EditText thisEt, EditText nextEt) {
                this.thisEt = thisEt;
                this.nextEt = nextEt;
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //text 输入框中改变后的字符串信息
                //start 输入框中改变后的字符串的起始位置
                //before 输入框中改变前的字符串的位置 默认为0
                //count 输入框中改变后的一共输入字符串的数量
                if (before == 0 && count == 1) {
                    thisEt.clearFocus();
                    nextEt.requestFocus();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        }

        //自定义键盘监听
        class MyOnKeyListener implements View.OnKeyListener {
            private EditText upDt, thisDt;

            public MyOnKeyListener(@Nullable EditText upDt, @Nullable EditText thisDt) {
                this.upDt = upDt;
                this.thisDt = thisDt;

            }

            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                Log.i("键盘监控", "view:" + view + "   i:" + i + "   keyEvent:" + keyEvent.toString());
                if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && i == KeyEvent.KEYCODE_DEL) {  //如果上一个输入框不为空
                    if (upDt != null && thisDt.getText().toString().isEmpty()) {
                        thisDt.clearFocus();
                        upDt.requestFocus();
                    }
                    return false;
                }
                return false;
            }
        }

        // 设置取消按钮文字和监听
        public Builder setButtonCancel(View.OnClickListener listener) {
            mButtonCancelClickListener = listener;
            return this;
        }

        // 设置确认按钮文字和监听
        public Builder setButtonConfirm(View.OnClickListener listener) {
            mButtonConfirmClickListener = listener;
            return this;
        }

        // 创建对话框
        public PhoneDialog create() {
            btnCancel.setOnClickListener(new android.view.View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mDialog.dismiss();
                    mButtonCancelClickListener.onClick(view);
                }
            });

            btnConfirm.setOnClickListener(new android.view.View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mDialog.dismiss();
                    mButtonConfirmClickListener.onClick(view);
                }
            });

            mDialog.setContentView(mLayout);
            mDialog.setCancelable(true);
            mDialog.setCanceledOnTouchOutside(false);
            return mDialog;
        }
    }
}
